/*
 * @Author: liuxiao
 * @Date: 2024-02-02 00:56:57
 * @Last Modified by: liuxiao
 * @Last Modified time: 2024-02-04 02:06:02
 * @ps:课程管理模块的相关接口
 */

import request from '@/utils/request'
import { ElMessage } from 'element-plus'

import { courseInfoData, chapterInfoData, lessonInfoData } from './type'
//课程管理模块接口地址
enum API {
  //获取课程列表信息接口
  GETCOURSELIST_URL = '/yyx/course/getCourseList',
  //保存课程信息接口
  SAVECOURSEINFO_URL = '/yyx/course/saveCourseInfo',
  //通过id获取单门课程信息
  GETCOURSEBYID_URL = '/yyx/course/getCourseInfoById',
  //获取章节和课时信息,ch代表chapter,le代表lesson
  GETCHANDLEBYCOURSEID_UR = '/yyx/course/getChAndLeByCourseId',
  //获取所有章节信息
  GETCHAPTERBYID_URL = '/yyx/course/getChapterById',
  //新建或更新章节信息接口
  SAVEORUPDATECHAPTER_URL = '/yyx/course/saveOrUpdateChapter',
  //更新章节状态接口
  UPDATECHAPTERSTATUS_URL = '/yyx/course/updateChapterStatus',
  //新建或更新章节信息接口
  SAVEORUPDATELESSON_URL = '/yyx/course/saveOrUpdateLesson',
  //删除课时接口
  DElETELESSON_URL = '/yyx/course/lesson',
  //删除单个课程
  DElETECOURSE_URL = '/yyx/course/course',
  //批量删除课程
  DElETECOURSES_URL = '/yyx/course/courses'
}

//获取课程列表的接口方法
//page:获取第几页 ---默认第一页
//limit:获取几门课程的数据
export const reqCourseList = async (
  pageNum: number = 1,
  pageSize: number = 3
) =>
  await request.get<any, any>(
    API.GETCOURSELIST_URL + `/?pageNum=${pageNum}&pageSize=${pageSize}`
  )

//保存课程信息方法
export const saveCourseInfo = async (courseInfo: courseInfoData) =>
  request.post<any, any>(API.SAVECOURSEINFO_URL, courseInfo)

//通过ID获取课程信息
export const getCourseInfoById = async (id: number) => {
  // try {
  //   const res = await request.get<any, any>(
  //     API.GETCOURSEBYID_URL + `/?id=${id}`
  //   )
  //   return res
  // } catch (err) {
  //   ElMessage({
  //     type: 'error',
  //     message: '获取单个课程信息失败!'
  //   })
  // }

  const res = await request.get<any, any>(API.GETCOURSEBYID_URL + `/?id=${id}`)
  return res
}

//获取章节和课时信息,ch代表chapter,le代表lesson
export const getChAndLeByCourseId = async (courseId: number) => {
  try {
    const res = await request.get<any, any>(
      API.GETCHANDLEBYCOURSEID_UR + `/?courseId=${courseId}`
    )
    return res
  } catch (err) {
    ElMessage({
      type: 'error',
      message: '获取章节课时信息失败!'
    })
  }
}

//通过章节ID获取章节信息
export const getChapterById = (id: number) => {
  try {
    return request<any, any>({
      method: 'GET',
      url: API.GETCHAPTERBYID_URL,
      params: { id }
    })
  } catch (err) {
    ElMessage({
      type: 'error',
      message: '获取章节信息失败!'
    })
  }
}

//新建或者更新-章节信息
//通过章节ID获取章节信息
export const saveOrUpdateChapter = (chapterInfo: Partial<chapterInfoData>) => {
  try {
    return request<any, any>({
      method: 'POST',
      url: API.SAVEORUPDATECHAPTER_URL,
      data: chapterInfo
    })
  } catch (err) {
    ElMessage({
      type: 'error',
      message: '新增或更新章节信息失败!'
    })
  }
}

//通过章节ID获取章节信息
export const updateChapterStatus = (chapterInfo: Partial<chapterInfoData>) => {
  // try {
  //   return request<any, any>({
  //     method: 'POST',
  //     url: API.SAVEORUPDATECHAPTER_URL,
  //     data: chapterInfo
  //   })
  // } catch (err) {
  //   ElMessage({
  //     type: 'error',
  //     message: '更新章节状态失败!'
  //   })
  // }

  return request<any, any>({
    method: 'POST',
    url: API.SAVEORUPDATECHAPTER_URL,
    data: chapterInfo
  })
}
//新建或者更新-课时信息
//通过章节ID获取课时信息
export const saveOrUpdateLesson = (lessonInfo: Partial<lessonInfoData>) => {
  return request<any, any>({
    method: 'POST',
    url: API.SAVEORUPDATELESSON_URL,
    data: lessonInfo
  })
}

//删除课时信息
export const deleteLesson = (id: number) => {
  return request<any, any>({
    method: 'DELETE',
    url: API.DElETELESSON_URL,
    params: { id }
  })
}

//删除单个课程
export const deleteCourseById = (id: number) => {
  return request<any, any>({
    method: 'DELETE',
    url: API.DElETECOURSE_URL,
    params: { id }
  })
}

//批量课程
export const deleteCoursesByIds = (listData: []) => {
  return request<any, any>({
    method: 'DELETE',
    url: API.DElETECOURSES_URL,
    data: { list: listData }
  })
}
